//
//  AlertPwdController.m
//  XiaoMiao
//
//  Created by jinglian on 2018/2/10.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "AlertPwdController.h"
#import "AlertPwdSmsCodeController.h"

@interface AlertPwdController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *refreshPwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *refreshButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;

@end

@implementation AlertPwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"修改密码";
    
    [self.refreshButton addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.nextButton addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonDidClick:(UIButton *)sender {
    if (sender == self.refreshButton) {
        sender.selected = !sender.isSelected;
        self.refreshPwdTextField.secureTextEntry = sender.isSelected;
        
    }else if (sender == self.nextButton) {
        [self.view endEditing:YES];
        
        NSString *msg = [self validateIsCanNext];
        if (msg.length != 0) {
            [KLProgressHUD showPromptHUDWithMessage:msg];
            return;
        }
        
        WEAKSELF
        KLProgressHUD *progressHUD = [KLProgressHUD showWaitingHUDWithSuperView:self.view message:@"发送短信中.."];
        [AppHttpClient getCodeWithPhone:[AppGlobal getLoginAccount] completion:^(BOOL success, NSString *errorMsg) {
            progressHUD.hidden = YES;
            if (success) {
                AlertPwdSmsCodeController *SMS = [[AlertPwdSmsCodeController alloc] initWithOldPwd:self.oldPwdTextField.text refreshPwd:self.refreshPwdTextField.text];
                [weakSelf.navigationController pushViewController:SMS animated:YES];
            }else {
                [KLProgressHUD showErrorHUDWithMessage:errorMsg];
            }
        }];
    }
}

- (NSString *)validateIsCanNext {
    if ([NSString app_isBlankString:self.oldPwdTextField.text]) {
        return @"请输入旧密码";
    }
    
    if ([NSString app_isBlankString:self.refreshPwdTextField.text]) {
        return @"请输入新密码";
    }
    
    if (self.oldPwdTextField.text.length > 13 || self.oldPwdTextField.text.length < 6) {
        return @"请输入6-12位旧密码";
    }
    
    if (self.refreshPwdTextField.text.length > 13 || self.oldPwdTextField.text.length < 6) {
        return @"请输入6-12位新密码";
    }
    
    return nil;
}

@end
